| Conditions | 1 |
| Paths | 8 |
| Total Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* globals versionCompare, alert */ |
||
| 15 | function($state, state, data) { |
||
| 16 | this.initSave = function() { |
||
| 17 | state.player = {}; |
||
| 18 | this.versionControl(); |
||
| 19 | state.init(); |
||
| 20 | $state.go('generators'); |
||
| 21 | }; |
||
| 22 | |||
| 23 | this.save = function() { |
||
| 24 | state.player.last_login = Math.floor(Date.now() / 1000); |
||
| 25 | localStorage.setItem('player', JSON.stringify(state.player)); |
||
| 26 | }; |
||
| 27 | |||
| 28 | this.load = function() { |
||
| 29 | try { |
||
| 30 | let storedPlayer = localStorage.getItem('player'); |
||
| 31 | if (!storedPlayer) { |
||
| 32 | this.initSave(); |
||
| 33 | } else { |
||
| 34 | state.player = JSON.parse(storedPlayer); |
||
| 35 | this.versionControl(); |
||
| 36 | } |
||
| 37 | } catch (err) { |
||
| 38 | alert('Error loading savegame, reset forced.'); |
||
| 39 | this.initSave(); |
||
| 40 | } |
||
| 41 | }; |
||
| 42 | |||
| 43 | this.versionControl = function() { |
||
| 44 | // delete saves older than this version |
||
| 45 | if (state.player.version && versionCompare(state.player.version, '2.6.0') < 0) { |
||
| 46 | state.player = {}; |
||
| 47 | } |
||
| 48 | // we merge the properties of the player with the start player to |
||
| 49 | // avoid undefined errors with new properties |
||
| 50 | state.player = angular.merge({}, data.start_player, state.player); |
||
| 51 | |||
| 52 | for (let resource in state.player.resources) { |
||
| 53 | if (!data.resources[resource]) { |
||
| 54 | delete state.player.resources[resource]; |
||
| 55 | continue; |
||
| 56 | } |
||
| 57 | if (state.player.resources[resource] && typeof state.player.resources[resource].number !== 'undefined') { |
||
| 58 | if (state.player.resources[resource].unlocked) { |
||
| 59 | state.player.resources[resource] = state.player.resources[resource].number; |
||
| 60 | } else { |
||
| 61 | state.player.resources[resource] = null; |
||
| 62 | } |
||
| 63 | } |
||
| 64 | } |
||
| 65 | for (let slot of state.player.element_slots) { |
||
| 66 | if (!slot) { |
||
| 67 | continue; |
||
| 68 | } |
||
| 69 | for (let i in slot.redoxes) { |
||
| 70 | if (slot.redoxes[i].from === -2 || slot.redoxes[i].to === -2) { |
||
| 71 | slot.redoxes.splice(i, 1); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | }; |
||
| 76 | } |
||
| 77 | ]); |
||
| 78 |